﻿using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
using seejee_Backendium.Core.Models;

namespace seejee_Backendium.Data.DbContest
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<Menu> Menus { get; set; }
        public DbSet<User> Users { get; set; }

        public DbSet<UserInfoDTO> UserInfoDTOs { get; set; }
        public DbSet<UserNameDTO> UserNameDTOs { get; set; }
        public DbSet<OperatorNameDTO> OperatorNameDTOs { get; set; }
        //用户APP信息
        public DbSet<UserApp> userApps { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

            modelBuilder.Entity<Menu>(entity =>
            {
                entity.ToTable("Menus");
                entity.HasKey(e => e.Id);

                entity.HasOne(m => m.Parent)
                    .WithMany(m => m.Children)
                    .HasForeignKey(m => m.ParentId)
                    .OnDelete(DeleteBehavior.Restrict);

                entity.HasIndex(m => m.ParentId);
                entity.HasIndex(m => m.Index);
            });

            modelBuilder.Entity<UserApp>(entity =>
            {
                entity.ToTable("UserApps");
                entity.HasKey(ua => ua.id);

                entity.Property(ua =>ua.userName)
                      .IsRequired()
                      .HasMaxLength(50);

                entity.Property(ua => ua.AppInformation)
                      .IsRequired()
                      .HasMaxLength(50);

                entity.Property(ua => ua.appName)
                      .IsRequired()
                      .HasMaxLength(50);


                entity.Property(ua => ua.updateTime)
                      .IsRequired()
                      .HasMaxLength(50);


                entity.Property(ua => ua.appAddress)
                      .IsRequired()
                      .HasMaxLength(50);
            });

            modelBuilder.Entity<UserNameDTO>(entity =>
            {
                entity.ToTable("CusUserList"); // 指定表名
                entity.HasKey(e => e.cCusName);      // 指定主键
            });
            modelBuilder.Entity<OperatorNameDTO>(entity =>
            {
                entity.ToTable("SEBI_Operator"); // 指定表名
                entity.HasKey(e => e.cOperatorName);      // 指定主键
            });

        }
    }
}